package servlet.user;

import entities.Comment;
import entities.Event;
import entities.User;
import exception.ValidationException;
import java.io.IOException;
import java.util.Vector;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import managers.EntityManagerAux;

/**
 * Commenter un event
 */
public class CommentEvent extends HttpServlet {
    @EJB
    private EntityManagerAux em;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        HttpSession session = request.getSession();
        String u =  (String) session.getAttribute("id");
        if (!u.isEmpty() && !u.equals("")) {
            String message = request.getParameter("message");
            String eventId = request.getParameter("eventId");
            String userId = request.getParameter("userId");
            Event event = (Event)request.getSession().getAttribute("nEvent");
			request.getSession().setAttribute("nEvent",event);
            if (!message.equals(null) || !message.equals("")) {
                try {
                    Comment c = em.creerComment(eventId, userId, message);
                    Vector<Comment> comments = new Vector<Comment>();
                    comments.addAll(em.searchCommentsByEvent(eventId));
                    //Integer rateMoyenne = eventSessionBean.moyenneRatesByEvent(eventId);
                    Event t = em.chercherEvent(eventId);
                    if (comments.equals(null)) {
                        request.setAttribute("comments", new Vector<Comment>());
                    } else {
                        request.setAttribute("comments", comments);
                    }

                    request.setAttribute("event", t);
                    //request.setAttribute("rateMoyenne", rateMoyenne);
                    request.getRequestDispatcher("showEvent2.jsp").forward(request, response);
                } catch(ValidationException e) {
                    request.setAttribute("rights",e.getMessage());
                    request.getRequestDispatcher("index.jsp").forward(request, response);
                }
            } else {
                request.setAttribute("rights","vous ne pouvez pas poster de comment vide");
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("message", "Vous devez avoir un compte client pour commenter un event");
            request.getRequestDispatcher("addUser.jsp").forward(request, response);
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}